package homework04;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class MiGong {
	// 随机生成5*5地图，0可以通行，1不能通行,输入wasd操作
	public static void main(String[] args) {
		Random r = new Random();
		String[][] arr = new String[5][5];
		for (int i = 0; i < arr.length; i++) { // 生成地图
			for (int j = 0; j < arr[i].length; j++) {
				arr[i][j] =String.valueOf(r.nextInt(2));
			}
		}

		Player P =new Player();
		arr[0][0] = P.getName();// 标记位置
		arr[arr.length - 1][arr[arr.length - 1].length - 1] = "0";
		int local_r = 0;
		int local_l = 0;
		printMap(arr);
		System.out.println("ready go!输入（out）可以直接退出");
		System.out.println("输入awds以移动，输入check以查看个人状态");
		Scanner sc = new Scanner(System.in);
		while (arr[arr.length - 1][arr[arr.length - 1].length - 1] != P.getName()) {// 为2结束
			String move = sc.next();
			switch (move) {
			case "w":
				if(local_r==0||arr[local_r-1][local_l].equals("1"))
					{System.out.println("这里不能通行！");
					continue;
					}
				else {
				arr[local_r][local_l] = "0";
				local_r--;
				break;
				}
			case "a":
				if(local_l==0||arr[local_r][local_l-1].equals("1"))
				{System.out.println("这里不能通行！");
				continue;
				}
			else {
				arr[local_r][local_l] = "0";
				local_l--;
				break;
			}
			case "s":
				if(local_r==arr.length-1||arr[local_r+1][local_l].equals("1"))
				{System.out.println("这里不能通行！");
				break;
				}
			else {
				arr[local_r][local_l] = "0";
				local_r++;
				break;
			}
			case "d":
			
				if(local_l==arr[arr.length-1].length-1||arr[local_r][local_l+1].equals("1"))
				{System.out.println("这里不能通行！");
				break;
				}
			else {
				arr[local_r][local_l] = "0";
				local_l++;
				break;
			}	
			case "out":
				break;
			case "check":
				System.out.println("生命值："+P.getLife());
				System.out.println("生命值上限："+P.getLifesize());
				System.out.println("攻击力："+P.getAttack());
				System.out.println("防御力："+P.getDefense());
				System.out.println("武器："+P.wp.getName());
				System.out.println("盔甲："+P.ar.getName());
			default:
				System.out.println("输入有误，请重新输入：");
				continue;
			}
//			if (local_r == -1 || local_r == arr.length || local_c == -1 || local_c == arr[local_r].length) {
//				System.out.println("越界，请重新游戏！");
//				return;
//          }
			
			arr[local_r][local_l] = P.getName();
			if(move.equals("out"))
				break;
			printMap(arr);
			RandomMake X=new RandomMake();
			switch(X.Un)
			{
			case 0:
				System.out.println("你什么都没遇到");
				break;
			case 1:
				System.out.println("怪物!");
				Monster newmon =getRandomMonster(X.redomMonster);
				System.out.println("你是否要战斗（是/否）");
				String Msanswer =sc.next();
				if(Msanswer.equals("是"))
				{
					fight(P,newmon);
					break;
				}
				else {
					System.out.println("你选择了逃跑。。。");
				break;
				}
			case 2:
				System.out.println("你获得了药水");
				Medicine newmed =getRandomMedicine(X.redomMedicine);
				System.out.println("你是否使用（是/否）");
				String Manswer =sc.next();
				if(Manswer.equals("是"))
				{
					if(P.getLife()+newmed.getAddlife()>P.getLifesize())
					{
						P.setLife(P.getLife());
						System.out.println("使用成功，你的血量是"+P.getLife()+"/"+P.getLifesize());
					}
					else
						{P.setLife(P.getLife()+newmed.getAddlife());
						System.out.println("使用成功，你的血量是"+P.getLife()+"/"+P.getLifesize());
						}
					break;
				}
				else
					break;
			case 3:
				System.out.println("你获得了武器!");
				Weapon newwp=getRandomWeapon(X.redomWeapon);
				System.out.println("你是否装备（是/否）");
				String Wanswer =sc.next();
				if(Wanswer.equals("是"))
				{
					P.wp=newwp;
					System.out.println("装备成功");
				}
				else
					break;
				break;
				
			case 4:
				System.out.println("你获得了盔甲");
				Armour newar=getRandomArmour(X.redomArmour);
				System.out.println("你是否装备（是/否）");
				String Aanswer =sc.next();
				if(Aanswer.equals("是"))
				{
					P.ar=newar;
					System.out.println("装备成功");
				}
				else
					break;
				break;
			}

		}
		System.out.println("end!");
	}

	public static void printMap(String[][] arr) {
		for (int i = 0; i < arr.length; i++) {
			System.out.println(Arrays.toString(arr[i]));
		}
	}
	public static Weapon getRandomWeapon(int a)
	{
		Weapon A =new Weapon(13, "小刀");
		Weapon B =new Weapon(17, "长剑");
		Weapon C =new Weapon(29, "双手剑");
		if(a==0)
			{System.out.println(A.getName());
			return A;
			}
		if(a==1)
		{System.out.println(B.getName());
		return B;
		}
		if(a==2)
		{System.out.println(C.getName());
		return C;
		}
		return A;
	}
	public static Armour getRandomArmour(int a)
	{
		Armour A =new Armour(5, "藤甲");
		Armour B =new Armour(10, "铁甲");
		Armour C =new Armour(30, "金甲");
		if(a==0)
		{System.out.println(A.getName());
		return A;
		}
		if(a==1)
		{System.out.println(B.getName());
		return B;
		}
		if(a==2)
		{System.out.println(C.getName());
		return C;
		}
		return A;
	}
	public static Medicine getRandomMedicine(int a)
	{
		Medicine A =new Medicine(20,"小型回复药水");
		Medicine B =new Medicine(50,"大型回复药水");
		Medicine C =new Medicine(100,"超级回复药水");
		if(a==0)
		{System.out.println(A.getName());
		return A;
		}
		if(a==1)
		{System.out.println(B.getName());
		return B;
		}
		if(a==2)
		{System.out.println(C.getName());
		return C;
		}
		return A;
	}
	public static Monster getRandomMonster(int a)
	{
		Monster A =new Monster("小鸡",10,0,1);
		Monster B =new Monster("母鸡",30,5,10);
		Monster C =new Monster("公鸡",60,15,20);
		if(a==0)
		{System.out.println(A.getName());
		return A;
		}
		if(a==1)
		{System.out.println(B.getName());
		return B;
		}
		if(a==2)
		{System.out.println(C.getName());
		return C;
		}
		return A;
	}
	public static void fight(Player P, Monster M)
	{
		int i=0;
		Scanner sc = new Scanner(System.in);
		while(P.getLife()>0||M.getLife()>0) {
			i++;
			System.out.println("第"+i+"回合");
			System.out.println("你是否逃跑（是/否）");
			String Fanswer =sc.next();
			if(Fanswer.equals("是"))
			{
				break;
			}
			else
			{
				System.out.println("你发起了攻击,造成了"+gethurt(P.getAttack(),M.getDefense())+"点伤害");
				M.setLife(M.getLife()-gethurt(P.getAttack(),M.getDefense()));
				if(M.getLife()<=0)
				{
					System.out.println(M.getName()+"生命=0，胜利！");
					break;
				}
				System.out.println(M.getName()+"发起了攻击,造成"+gethurt(M.getAttack(),P.getDefense()));
				P.setLife(P.getLife()-gethurt(M.getAttack(),P.getDefense()));
				System.out.println("你的生命值："+P.getLife());
				System.out.println(M.getName()+"的生命值："+M.getLife());
				if(P.getLife()<=0)
				{
					System.out.println("你输了");
					break;
				}
			}
		}
	}
	public static int gethurt(int A ,int D) {
		int hurt =A-D;
		if(hurt<0)
			hurt=0;
		return hurt;
	}
}
